package com.lagou.cl.utils;

import com.lagou.cl.annotation.MyAutowired;
import com.lagou.cl.annotation.MyComponet;

import java.sql.SQLException;


/**
 * @author 程  林
 * @date 2020-04-01 10:26
 * @description //事务管理器类：负责手动事务的开启、提交、回滚
 * @since V1.0.0
 */
@MyComponet
public class TransactionManager {

	/**
	 * 事务管理器需要注入connection连接
	 */

	@MyAutowired
	private ConnectionUtils connectionUtils;

	/**
	 * 开启手动事务控制
	 *
	 * @throws SQLException
	 */
	public void beginTransaction() throws SQLException {
		connectionUtils.getCurrentThreadConn().setAutoCommit(false);
	}


	/**
	 * 提交事务
	 *
	 * @throws SQLException
	 */
	public void commit() throws SQLException {
		connectionUtils.getCurrentThreadConn().commit();
	}


	/**
	 * 回滚事务
	 *
	 * @throws SQLException
	 */
	public void rollback() throws SQLException {
		connectionUtils.getCurrentThreadConn().rollback();
	}
}
